from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from schemas import UserRequest, UserResponse

app = FastAPI()

# 允许跨域访问（Flutter 请求需要）
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 上线后建议换成具体域名
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/welcome", response_model=UserResponse)
def welcome_user(user: UserRequest):
    if user.age < 18:
        age_group = "未成年"
    elif user.age < 60:
        age_group = "成年人"
    else:
        age_group = "老年人"
    message = f"你好，{user.name}！您属于 {age_group}。欢迎使用我们的服务。"
    return UserResponse(message=message)